package com.cswangdao.service.impl;

import com.cswangdao.domain.dto.TopicPageDTO;
import com.cswangdao.domain.model.MarketGoods;
import com.cswangdao.domain.model.MarketTopic;
import com.cswangdao.domain.vo.TopicDetailVo;
import com.cswangdao.domain.vo.TopicGoodsVo;
import com.cswangdao.mapper.MarketGoodsMapper;
import com.cswangdao.mapper.MarketTopicMapper;
import com.cswangdao.result.PageResult;
import com.cswangdao.service.TopicService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

@Service
@Transactional
public class TopicServiceImpl implements TopicService {

    @Autowired
    MarketTopicMapper topicMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public PageResult<MarketTopic> pageQueryInfo(TopicPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getLimit());
        Page<MarketTopic> page = topicMapper.pageQueryInfo(pageDTO);
        PageResult<MarketTopic> result = PageResult.result(page);
        return result;

    }

    @Override
    public TopicDetailVo queryById(String id) {
        TopicDetailVo topicDetailVo = new TopicDetailVo();
        MarketTopic topic = topicMapper.queryById(id);
        topicDetailVo.setTopic(topic);
        if(topic.getGoods() == null || topic.getGoods().length == 0){
            return topicDetailVo;
        }
       // List<Integer> list = Stream.of(topic.getGoods()).map(Integer::parseInt).toList();
        List<Integer> list = Arrays.asList(topic.getGoods());
        List<TopicGoodsVo> goodsList =goodsMapper.queryByIds(list);
        topicDetailVo.setGoodsList(goodsList);
        return topicDetailVo;
    }

    @Override
    public void update(MarketTopic topic) {
        topicMapper.updateById(topic);
    }

    @Override
    public void addTopic(MarketTopic topic) {
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        topicMapper.addTopic(topic);
    }

    @Override
    public void delete(Integer id) {
        topicMapper.deleteById(id);
    }

    @Override
    public void batchDelete(List<Integer> ids) {
        topicMapper.batchDelete(ids);
    }


}
